package ferp.android.widgets.buttons;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;

/**
 * User: igorgok
 * Date: 12/30/13 12:25 PM
 */
public class TextButton extends TwoLayerButton
{
  public TextButton(Context context) {this(context, null, 0);}
  public TextButton(Context context, AttributeSet attrs) {this(context, attrs, 0);}
  public TextButton(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);

    if (attrs != null)
    {
      // read button text
      TypedArray ta = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.text});

      // assign the text
      text.setText(ta.getString(0));
      ta.recycle();
    }
  }

  // set text
  public final void set(CharSequence s) {text.setText(s);}

  // set colors
  public void setTextColor(int color) {text.setTextColor(color);}
  public final void set(ColorStateList csl) {text.setTextColor(csl);}
  // set shadow
  public final void set(int color, float radius, float dx, float dy) {text.setShadowLayer(radius, dx, dy, color);}

  // get caption text size
  public float getTextSize() {return text.getTextSize();}

  @Override
  public final void setEnabled(boolean enabled)
  {
    super.setEnabled(enabled);

    text.setTextColor(enabled ? getNormalTextColor() : getDisabledTextColor());
  }

  protected int getNormalTextColor() {return GUI.Color.NORMAL;}
  protected int getDisabledTextColor() {return GUI.Color.DISABLE;}

  @Override
  protected final View onCreateUpperLayer(Context context, AttributeSet attrs)
  {
    return (text = new Text(context));
  }

  @Override
  protected int getButtonBackgroundResource()
  {
    return R.drawable.selector_button_intro;
  }

  // default Y scale factor
  private static final float SCALE_FACTOR_Y = 0.4f;

  // text view
  private class Text extends TextView
  {
    public Text(Context context)
    {
      super(context);

      setTypeface(GUI.Font.BOLD);
      // default colors
      setTextColor(csl);
      setGravity(Gravity.CENTER);
      setMaxLines(2);
      setText("", TextView.BufferType.SPANNABLE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);

      // try to set the text size
      if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY)
      {
        // read text Y scale factor from layout_weight
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)TextButton.this.getLayoutParams();
        // set the Y scale factor
        float fy = lp == null || lp.weight == 0 ? SCALE_FACTOR_Y : lp.weight;

        //Log.debug(Log.TAG, "TB - tv height " + text.getHeight() + '/' + text.getMeasuredHeight() + ", text size " + text.getTextSize());
        if (GUI.FitText.byHeightAndWidth(this, getMeasuredHeight() * fy, getMeasuredWidth() * 0.85f))
        {
          // measure again
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);

          //Log.debug(Log.TAG, "TB - new text size " + text.getTextSize());
        }
      }
    }
  }

  // default color state list
  private static final ColorStateList csl = new ColorStateList(new int[][] {
                                                                   new int[] { android.R.attr.state_enabled}, // enabled
                                                                   new int[] {-android.R.attr.state_enabled}, // disabled
                                                                   //new int[] { android.R.attr.state_pressed}  // pressed
                                                               },
                                                               new int[] {
                                                                   GUI.Color.NORMAL,
                                                                   GUI.Color.DISABLE,
                                                               });

  // text view
  private Text text;
}
